package com.zy.dao.impl;

import com.zy.bean.UserBean;
import com.zy.dao.UserDao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LittleDesire
 * @description
 * @date 2022-04-06 周三 11:31
 * @since 17
 */
public class UserDaoImpl implements UserDao {

    private static final String DRIVERNAME = null;
    private static final String URL = null;
    private static final String USERNAME = null;
    private static final String PASSWORD = null;

    private String sql;
    Connection conn;
    PreparedStatement ps;
    int rows;

    @Override
    public int add(UserBean user) {
        try {
            Class.forName(DRIVERNAME);
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            sql = "insert into * from ...";
            ps = conn.prepareStatement(sql);
            rows = ps.executeUpdate();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return rows;
    }

    @Override
    public int update(UserBean user) {
        try {
            Class.forName(DRIVERNAME);
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            sql = "update * from ...";
            ps = conn.prepareStatement(sql);
            rows = ps.executeUpdate();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return rows;
    }

    @Override
    public int delete(UserBean user) {
        try {
            Class.forName(DRIVERNAME);
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            sql = "delete * from ...";
            ps = conn.prepareStatement(sql);
            rows = ps.executeUpdate();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return rows;
    }

    @Override
    public List<UserBean> quarrAll() {
        ResultSet rs = null;
        List<UserBean> list = new ArrayList<>();
        try {
            Class.forName(DRIVERNAME);
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            sql = "select * from ...";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if (rs.next()) {
                Object o = rs.getObject(1);
                list.add((UserBean) o);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}
